package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.HX;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.view.ZoomImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ImageViewHouseTypeActivity extends BaseActivity {

    @BindView(R.id.iv_back_activity_image_view)
    ImageView mIvBackActivityImageView;
    @BindView(R.id.tv_title_activity_image_view)
    TextView mTvTitleActivityImageView;
    @BindView(R.id.viewpager_activity_image)
    ViewPager mViewpagerActivityImage;
    @BindView(R.id.tv_type_activity_image_view)
    TextView mTvTypeActivityImageView;
    @BindView(R.id.tv_details_activity_image_view)
    TextView mTvDetailsActivityImageView;
    @BindView(R.id.ll_botton_activity_image_view)
    LinearLayout mLlBottonActivityImageView;
    private int mChekcPosition;
    private ArrayList<HX> mHXES;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_image_view);
    }

    /**
     * 户型图开启的方式
     *
     * @param context
     * @param hxArrayList    数据
     * @param checkPosition 选中的图片顺序
     */
    public static void startActivity(Context context, ArrayList<HX> hxArrayList, int checkPosition) {
        Intent intent = new Intent(context, ImageViewHouseTypeActivity.class);
        intent.putParcelableArrayListExtra(Contans.INTENT_DATA, hxArrayList);
        intent.putExtra("chekcPosition", checkPosition);
        context.startActivity(intent);
    }


    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        mChekcPosition = intent.getIntExtra("chekcPosition", 0);
        mHXES= intent.getParcelableArrayListExtra(Contans.INTENT_DATA);
        ImagAdapter imagAdapter = new ImagAdapter();
        mViewpagerActivityImage.setAdapter(imagAdapter);
        mViewpagerActivityImage.setCurrentItem(mChekcPosition, false);
        mTvTitleActivityImageView.setText(mChekcPosition + 1 + "/" + mHXES.size());

        mTvTypeActivityImageView.setText(mHXES.get(mChekcPosition).getModel()+"");
        mTvDetailsActivityImageView.setText(mHXES.get(mChekcPosition).getSummary()+"    "+mHXES.get(mChekcPosition).getArea());

        mViewpagerActivityImage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                mTvTitleActivityImageView.setText(position + 1 + "/" + mHXES.size());
                mTvTypeActivityImageView.setText(mHXES.get(position).getModel()+"");
                mTvDetailsActivityImageView.setText(mHXES.get(position).getSummary()+"    "+mHXES.get(position).getArea());






            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.iv_back_activity_image_view)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    private class ImagAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mHXES.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {

            ZoomImageView zoomImageView = new ZoomImageView(ImageViewHouseTypeActivity.this);
            //  zoomImageView.wi
            String s = mHXES.get(position).getRout();
            ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            zoomImageView.setLayoutParams(layoutParams);
            zoomImageView.setScaleType(ImageView.ScaleType.MATRIX);

            Glide.with(ImageViewHouseTypeActivity.this).load(s).into(zoomImageView);

            container.addView(zoomImageView);


            return zoomImageView;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

}
